package com.to_many;

import com.bean.Card;
import com.bean.Classes;
import com.bean.Student;
import com.to_one.CardMapper;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Test;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

public class test2 {
    @Test
    public void selectAll() throws IOException {
        InputStream is = Resources.getResourceAsStream("MyBatisConfig.xml");

        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);

        SqlSession sqlSession = sqlSessionFactory.openSession(true);

        ClassesMapper mapper = sqlSession.getMapper(ClassesMapper.class);

        List<Classes> list = mapper.selectAll();

        for (Classes classes : list) {
            System.out.println(classes.getId()+","+classes.getName());
            List<Student> students = classes.getStudents();
            for (Student student : students) {
                System.out.println("\t"+student.getId()+student.getName()+student.getAge());
            }
        }

        sqlSession.close();
        is.close();
    }
}
